﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2024/7/15
 * 时间: 13:17
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;

namespace hitokoto
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll", EntryPoint = "SetParent")]
        static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll")]
        static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect);
        [DllImport("user32.dll")]
        static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);

         
 
        
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//https://blog.csdn.net/Factor_/article/details/84799332
			
			InitializeComponent();
			label1.Text=GetWebClient("https://international.v1.hitokoto.cn/?c=i&encode=text");
			label1.BackColor=  Color.FromArgb(129,148,170);
			
			
			IntPtr hShell = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
            IntPtr hBar = FindWindowEx(hShell, IntPtr.Zero, "ReBarWindow32", null);
            IntPtr hMin = FindWindowEx(hBar, IntPtr.Zero, "MSTaskSwWClass", null);
				
            Rectangle rcMin= new Rectangle();   
            Rectangle rcShell= new Rectangle();   
            GetWindowRect(hMin, ref rcMin);
            GetWindowRect(hShell, ref rcShell);

            //Rectangle ScreenArea = System.Windows.Forms.Screen.GetWorkingArea(this);
            Rectangle ScreenArea = System.Windows.Forms.Screen.GetBounds(this);
            
            MoveWindow(hMin, 0, 0, rcMin.Right - rcMin.Left - this.Width, rcMin.Bottom - rcMin.Top, true);
			SetParent(this.Handle, hBar );
			MoveWindow(this.Handle, rcMin.Right - rcMin.Left  , (ScreenArea.Height - rcShell.Y - this.Height)/2 +2, this.Width, this.Height, true);


			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		private string GetWebClient(string url)
		{
        	ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
		    
		    string strHTML = "";
		    WebClient myWebClient = new WebClient();
		    Stream myStream = myWebClient.OpenRead(url);
		    StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
		    strHTML = sr.ReadToEnd();
		    myStream.Close();
		    return strHTML;
		}
		void Label1DoubleClick(object sender, EventArgs e)
		{
			label1.Text=GetWebClient("https://international.v1.hitokoto.cn/?c=i&encode=text");
		}
		void ExitToolStripMenuItemClick(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}
